$(function() {
    "use strict";

    $('.lang-menu li a').on('click',function(){
        var me = $(this);
        $.get(me.attr('data-url'),function(data){
            if(data.err==0){
                window.location.reload();
            }
        });
    });

    //买入委托操作

    $('#buyForm').validate({
        // event:'blur',
        errorElement: "span",
        errorClass  : "bg-warning",
        onkeyup: false,
        submitHandler: function(form){
            var me = $(form);
            var data = {
                'market_id':currency.market_id,
                'num':$('#buyNum').val(),
                'price':$('#buyPrice').val()
            };
            $('#buyConfirm').modal();
            $('#buyConfirm').on('shown.bs.modal', function (e) {
                $(e.target).find('.confirm_price').val( formatMoney(data.price,8,'','') );
                $(e.target).find('.confirm_num').val( formatMoney(data.num,8,'','') );
                $(e.target).find('.confirm_fee').val( formatMoney(currency['currency_buy_fee'],8,'','') );
                $(e.target).find('.confirm_volume').val( formatMoney( accMul(data.price,data.num) ,8,'','') );
                $(e.target).find('.confirm_btn').off('click').on('click',function(){
                    layer.load(def.dialogStyle);
                    $.post(me.attr('data-url'),data,function(data){
                        layer.closeAll('loading');
                        if(data.err==0){
                            $('#buyConfirm').modal('hide');
                        }else{
                            err(data.msg,function(){
                                $('#buyConfirm').modal('hide');
                            });
                        }
                    });
                });
            });
        }  
    });

    $('#sellForm').validate({
        // event:'blur',
        errorElement: "span",
        errorClass  : "bg-warning",
        onkeyup: false,
        submitHandler: function(form){
            var me = $(form);
            var data = {
                'market_id':currency.market_id,
                'num':$('#sellNum').val(),
                'price':$('#sellPrice').val()
            };
            $('#sellConfirm').modal();
            $('#sellConfirm').on('shown.bs.modal', function (e) {
                $(e.target).find('.confirm_price').val( formatMoney(data.price,8,'','') );
                $(e.target).find('.confirm_num').val( formatMoney(data.num,8,'','') );
                $(e.target).find('.confirm_fee').val( formatMoney(currency['currency_buy_fee'],8,'','') );
                $(e.target).find('.confirm_volume').val( formatMoney( accMul(data.price,data.num) ,8,'','') );
                $(e.target).find('.confirm_btn').off('click').on('click',function(){
                    layer.load(def.dialogStyle);
                    $.post(me.attr('data-url'),data,function(data){
                        layer.closeAll('loading');
                        if(data.err==0){
                            $('#sellConfirm').modal('hide');
                        }else{
                            err(data.msg,function(){
                                $('#sellConfirm').modal('hide');
                            });
                        }
                    });
                })
            })
        }  
    });


    //删除委托
    $('#myOrderList').on('click','.del',function(){
        var me = $(this);
        layer.load(def.dialogStyle);
        $.post(me.attr('data-url'),data,function(data){
            layer.closeAll('loading');
            if(data.err==0){
                
            }else{
                err(data.msg);
            }
        });
    });

    //添加银行卡
    $('.add-card-form').validate({
        errorElement: "span",
        errorClass  : "text-danger",
        submitHandler: function(form){
            var me = $(form);
                var data = {
                'info[bank_name]':$('#bank_name',me).val(),
                'info[card_name]':$('#card_name',me).val(),
                'info[card_num]':$('#card_num',me).val(),
                'info[card_branch]':$('#card_branch',me).val(),
                'info[remarks]':$('#remarks',me).val()
            };
            layer.load(def.dialogStyle);
            $.post(me.attr('data-action'),data,function(data){
                layer.closeAll('loading');
                if(data.err==0){
                    $('#addCard').modal('hide');
                    success(data.msg);
                }else{
                    err(data.msg);
                }
            });
        }  
    });

    //提现
    $('.withdraw-form').validate({
        errorElement: "span",
        errorClass  : "text-danger",
        submitHandler: function(form){
            var me = $(form);
            var data = {
                'info[bank_id]':$('#bank_id',me).val(),
                'info[withdraw_num]':$('#withdraw_num',me).val(),
            };
            layer.load(def.dialogStyle);
            $.post(me.attr('data-action'),data,function(data){
                layer.closeAll('loading');
                if(data.err==0){
                    success(data.msg,function(){
                        window.location.href=data.url;
                    });
                }else if(data.err==2){
                    def.auth_level = data.auth_level;
                    if(def.auth_level==1){
                        $('#Auth .googleInput').remove();
                    }
                    if(def.auth_level==2){
                        $('#Auth .emailInput').remove();
                    }
                    $('#Auth').modal({
                        backdrop:'static',
                        keyboard:false
                    });
                }else{
                    err(data.msg);
                }
            });
        }  
    });
    //提现
    $('.withdraw-rmb-auth').validate({
        errorElement: "span",
        errorClass  : "text-danger",
        submitHandler: function(form){
            var me = $(form);
            var data = {
                'info[auth_level]':def.auth_level?def.auth_level:0,
                'info[bank_id]':$('#bank_id',$('.withdraw-form')).val(),
                'info[withdraw_num]':$('#withdraw_num',$('.withdraw-form')).val(),
                'info[google_code]':$("[name='info[google_code]']",me).val(),
                'info[email_code]':$("[name='info[email_code]']",me).val()
            };
            layer.load(def.dialogStyle);
            $.post(me.attr('data-action'),data,function(data){
                layer.closeAll('loading');
                if(data.err==0){
                    success(data.msg,function(){
                        window.location.href=data.url;
                    });
                }else{
                    err(data.msg);
                }
            });
        }  
    });

    //google认证
    $('.google-auth-form').validate({
        errorElement: "span",
        errorClass  : "text-danger",
        submitHandler: function(form){
            var me = $(form);
            var data = {
                'sk':$('#skInput',me).val(),
                'code':$('#codeInput',me).val()
            };
            layer.load(def.dialogStyle);
            $.post(me.attr('data-action'),data,function(data){
                layer.closeAll('loading');
                if(data.err==0){
                    success(data.msg,function(){
                        window.location.reload();
                    });
                }else{
                    err(data.msg);
                }
            });
        }  
    });

    $('.login-form').validate({
        errorElement: "span",
        errorClass  : "text-danger",
        submitHandler: function(form){
            var me = $(form);
            var data = {
                'info[email]':$('#nickEmail',me).val(),
                'info[pwd]':$('#pwdInput',me).val()
            };
            layer.load(def.dialogStyle);
            $.post(me.attr('data-action'),data,function(data){
                layer.closeAll('loading');
                if(data.err==0){
                    if(data.action=='redirect'){
                        window.location.href=data.url;
                    }
                }else if(data.err==2){
                    def.auth_level = data.auth_level;
                    if(def.auth_level==1){
                        $('#Auth .googleInput').remove();
                    }
                    if(def.auth_level==2){
                        $('#Auth .emailInput').remove();
                    }
                    $('#Auth').modal({
                        backdrop:'static',
                        keyboard:false
                    });
                }else{
                    err(data.msg);
                }
            });
        }  
    });

    $('.login-auth').validate({
        errorElement: "span",
        errorClass  : "text-danger",
        submitHandler: function(form){
            var me = $(form);
            var data = {
                'info[auth_level]':def.auth_level?def.auth_level:0,
                'info[email]':$('#nickEmail').val(),
                'info[email]':$('#nickEmail').val(),
                'info[pwd]':$('#pwdInput').val(),
                'info[google_code]':$("[name='info[google_code]']",me).val(),
                'info[email_code]':$("[name='info[email_code]']",me).val()
            };
            layer.load(def.dialogStyle);
            $.post(me.attr('data-action'),data,function(data){
                layer.closeAll('loading');
                if(data.err==0){
                    success(data.msg,function(){
                        if(data.action=='redirect'){
                            window.location.href=data.url;
                        }
                    });
                }else{
                    err(data.msg,function(){
                        if(data.action=='reload'){
                            window.location.reload();
                        }
                    });
                }
            });
        }  
    });

    $('.user-personal-form').validate({
        errorElement: "span",
        errorClass  : "text-danger",
        submitHandler: function(form){
            var me = $(form);
            var data = {
                'info[nick]':$("[name='info[nick]']",me).val(),
                'info[name]':$("[name='info[name]']",me).val(),
                'info[birthday]':$("[name='info[birthday]']",me).val(),
                'info[country]':$("[name='info[country]']",me).val(),
                'info[province]':$("[name='info[province]']",me).val(),
                'info[city]':$("[name='info[city]']",me).val(),
                'info[area]':$("[name='info[area]']",me).val(),
                'info[address]':$("[name='info[address]']",me).val(),
                'info[phone]':$("[name='info[phone]']",me).val(),
            };
            layer.load(def.dialogStyle);
            $.post(me.attr('data-action'),data,function(data){
                layer.closeAll('loading');
                if(data.err==0){
                    success(data.msg,function(){
                        $('#personal').modal('hide');
                    });
                }else{
                    err(data.msg);
                }
            });
        }  
    });
    
    $('.send_repwd_email').on('click',function(){
        var me = $(this);
        layer.load(def.dialogStyle);
        $.post(me.attr('data-url'),function(data){
            layer.closeAll('loading');
            if(data.err==0){
                success(data.msg);
            }else{
                err(data.msg);
            }
        });
    })

    $('.change-pwd-form').validate({
        errorElement: "span",
        errorClass  : "text-danger",
        submitHandler: function(form){
            var me = $(form);
            var data = {
                'info[oldpwd]':$("[name='info[oldpwd]']",me).val(),
                'info[pwd]':$("[name='info[pwd]']",me).val(),
                'info[repwd]':$("[name='info[repwd]']",me).val(),
                'info[code]':$("[name='info[code]']",me).val(),
            };
            layer.load(def.dialogStyle);
            $.post(me.attr('data-action'),data,function(data){
                layer.closeAll('loading');
                if(data.err==0){
                    success(data.msg,function(){
                        $('#changePwd').modal('hide');
                        if(data.action=='redirect'){
                            window.location.href=data.url;
                        }
                    });
                }else{
                    err(data.msg);
                }
            });
        }  
    });

    $('.send_reg_email').on('click',function(){
        var me = $(this);
        layer.load(def.dialogStyle);
        $.post(me.attr('data-url'),{email:$('#emailInput').val()},function(data){
            layer.closeAll('loading');
            if(data.err==0){
                success(data.msg);
            }else{
                err(data.msg);
            }
        });
    })

    $('.reg-form').validate({
        errorElement: "span",
        errorClass  : "text-danger",
        errorPlacement: function(error, element) {
            if(element.hasClass('showErrParent')){
                error.appendTo(element.parent().parent());
            }else{
                error.appendTo(element.parent());
            }
        },
        submitHandler: function(form){
            $(form).submit();
        }  
    });

    $('.add_faq_form').validate({
        errorElement: "span",
        errorClass  : "text-danger",
        submitHandler: function(form){
            var me = $(form);
            var data = {
                'info[title]':$("[name='info[title]']",me).val(),
                'info[text]':$("[name='info[text]']",me).val(),
            };
            layer.load(def.dialogStyle);
            $.post(me.attr('data-action'),data,function(data){
                layer.closeAll('loading');
                if(data.err==0){
                    success(data.msg,function(){
                        window.location.reload();
                    });
                }else{
                    err(data.msg);
                }
            });
        }  
    });



})
